﻿using System.Collections.ObjectModel;
using System.Windows.Input;
using SnakeGame.Views;
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using SnakeGame.Models;

namespace SnakeGame.ViewModels
{
    class SelectPlayerViewModel : NotifyPropertyChanged
    {
        public ICommand SelectPlayerCommand { get; set; }
        public ICommand NextCommand { get; set; }
        public ICommand NewGameCommand { get; set; }

        //Edit config
        public ICommand EditConfigCommand { get; set; }

        public ICommand FullScreenCommand { get; set; }

        //public Visibility ExitVisibility { get; set; }
        private Visibility _exitVisibility;
        public Visibility ExitVisibility
        {
            get { return _exitVisibility; }
            set
            {
                _exitVisibility = value;
                OnPropertyChanged("ExitVisibility");
            }
        }

        private void OnEditConfigCommand(object obj)
        {
            EditConfigWindow ed = new EditConfigWindow();
            ed.Owner = _mainFrame.Window;
            ed.Initialize();
            ed.ShowDialog();
        }

        private MainFrame _mainFrame;

        public SelectPlayerViewModel()
        {
            SelectPlayerCommand = new RelayCommand(OnSelectPlayer);
            NextCommand = new RelayCommand(OnNextCommand);

            EditConfigCommand = new RelayCommand(OnEditConfigCommand);

            NewGameCommand = new RelayCommand(OnNewGameCommand);

            FullScreenCommand = new RelayCommand(OnFullScreenCommand);
        }

        private void OnFullScreenCommand(object obj)
        {
            if (_mainFrame.Window.WindowState == WindowState.Maximized)
            {
                _mainFrame.Window.WindowStyle = WindowStyle.SingleBorderWindow;
                _mainFrame.Window.ResizeMode = ResizeMode.CanResize;
                _mainFrame.Window.WindowState = WindowState.Normal;
            }
        }

        private void OnNewGameCommand(object obj)
        {
            _mainFrame.NewGame();
            _mainFrame.Window.StateChanged -= Window_StateChanged;
        }

        private void OnNextCommand(object obj)
        {
            if (_mainFrame.Players.Count == 0)
            {
                MessageWindow msg = new MessageWindow();
                msg.Owner = _mainFrame.Window;
                msg.Initialize("Please select at least one player.");
                msg.ShowDialog();
                //MessageBox.Show("Please select at least one player.");
                return;
            }

            GamePage g = new GamePage();
            _mainFrame.Navigate(g);
            g.Initialize(_mainFrame);
        }

        public void Initialize(MainFrame mainFrame)
        {
            _mainFrame = mainFrame;

            _mainFrame.NumberOfPlayer = 1;
            _mainFrame.Players = new ObservableCollection<Player>();

            ExitVisibility = (_mainFrame.Window.WindowState == WindowState.Maximized) ? Visibility.Visible : Visibility.Hidden;
            _mainFrame.Window.StateChanged += Window_StateChanged;

        }

        void Window_StateChanged(object sender, EventArgs e)
        {
            ExitVisibility = _mainFrame.Window.WindowState == WindowState.Maximized ? Visibility.Visible : Visibility.Hidden;
        }

        private void OnSelectPlayer(object obj)
        {
            var button = obj as System.Windows.Controls.Button;
            var id = button.Tag.ToString();
            PlayerPage p = new PlayerPage();
            p.Owner = _mainFrame.Window;
            p.Initialize(_mainFrame, id);
            p.ShowDialog();

            if (p.IsSelected)
            {
                // disable button
                button.IsEnabled = false;
                button.Content = p.PlayerName;
                
                //ImageBrush berriesBrush = new ImageBrush();
                //berriesBrush.ImageSource = new BitmapImage(new Uri("pack://application:,,,/Resources/L1.png"));
                Brush berriesBrush = button.Background;
                berriesBrush.Opacity = 0.5;

                // Use the brush to paint the button's background.
                button.Background = berriesBrush;
            }

            #region Go to Select Character page
            //if (obj == null)
            //{
            //    _mainFrame.NumberOfPlayer = 1;
            //}
            //else
            //{
            //    _mainFrame.NumberOfPlayer = int.Parse((string)obj);
            //}

            //_mainFrame.Players = new ObservableCollection<Player>();

            //for (int i = 1; i <= _mainFrame.NumberOfPlayer; ++i)
            //{
            //    Player p = new MainFrame.Player
            //    {
            //        Index = i - 1,
            //        Message = "Player " + i.ToString(),
            //    };
            //    _mainFrame.Players.Add(p);
            //}

            //CharacterPage cp = new CharacterPage();
            //_mainFrame.Navigate(cp);
            //cp.Initilize(_mainFrame);
            #endregion
        }
    }
}
